<?php
/**
 * 一次性脚本
 *
 */
class script extends exec
{
    function __construct()
    {
        parent::__construct();
        $this->loadModel('g5_certificate');
        $this->loadModel('g5_certificate');
        $this->loadModel('g5_perfile');
        $this->loadModel('g5_cron_job');
        $this->loadModel('g5_original_pack');
    }

    function run()
    {
        //echo '/Users/jufenghudong/www/package/originpak/*.ipa';


        $profile =  glob(PERFILE. "*.mobileprovision");

        foreach($profile as $val) {

            $proInfo = $this->_getPerInfo($val);

            $result = $this->g5_certificate->getOne('id', array('md5' => md5($proInfo['certificate'])));

            //var_dump($result); exit;

            $data = array(
                'name'              => basename($val),
                'sourceCertificate' => $result['id'],
                'appName'           => '请维护',
                'use'               => rand(1, 4),
                'addTime'           => time(),
                'addUser'           => '脚本自动',
                'bundleId'          => $proInfo['bundleId'],
                'perfileCertificate'=> $proInfo['certificate'],
                'itermid'           => $proInfo['itermid']
            );
            $this->g5_perfile->insert($data);

        }
        /**
        $originPath = glob(ORIGIN_PAK. "*.ipa");

        foreach($originPath as $key => $pack)
        {

            $data = array(
                'appName'  => basename($pack),
                'packName' => basename($pack),
                'packSize' => format_bytes(filesize($pack)),
                'packUrl'  => '--------',
                'addTime'  => time(),
                'addUser'  => '脚本自动'
            );
            $this->g5_original_pack->insert($data);

        } */



    }

    /**
     * 读取授权文件相关信息
     * @param $proFile 授权文件
     * @author Giles <giles.wang@icloud.com>
     * @return array()
     */
    function _getPerInfo($proFile)
    {
        if(!is_file($proFile)) {
            return false;
        }
        $cmdXml = "security cms -D -i $proFile > ".ROOT_PATH."web/temp.xml";
        shell_exec($cmdXml);

        $xmlData = simplexml_load_file(ROOT_PATH. "web/temp.xml");
        $string = (array) $xmlData->dict->string;

        $return['certificate'] = 'iPhone Distribution: '.$string[2];

        $bundleTemp = (array) $xmlData->dict->dict->string;
        $bundlitem = $bundleTemp[0];
        $temp = explode('.', $bundlitem);
        $return['itermid'] = $temp[0];
        unset($temp[0]);
        $return['bundleId'] = implode('.', $temp);
        if(empty($return['bundleId'])) {
            $bundlitem = $bundleTemp[2];
            $temp = explode('.', $bundlitem);
            $return['itermid'] = $temp[0];
            unset($temp[0]);
            $return['bundleId'] = implode('.', $temp);
        }

        return $return;
    }
}